package org.example.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import java.util.Random;

@Aspect
@Component
public class SecurityAspect {
    private Random random = new Random();

    // 权限检查：仅对add和delete方法生效
    @Before("execution(* org.example.service.OrderService.add(..)) || execution(* org.example.service.OrderService.delete(..))")
    public void checkPermission() {
        int permission = random.nextInt(2);  // 生成0（有权限）或1（无权限）
        if (permission == 1) {
            System.out.println("权限检查失败，禁止操作");
            throw new RuntimeException("没有权限执行此操作");  // 终止方法执行
        } else {
            System.out.println("权限检查通过，允许操作");
        }
    }
}
